/**
 * Created by zzy on 2016/11/13.
 */
var $ = require('jquery');
require('ueditor');
var ZeroClipboard = require('ueditor/third-party/zeroclipboard/ZeroClipboard');
window.ZeroClipboard = ZeroClipboard;
var $alert = require('libs/alert');
require('layout/util');
require('layout/uiJs');
require("./ajaxfileupload.js");
var news_category={"1":"新闻","2":"服务与帮助","3":"政策法规","4":"系统公告"};
var mapping={"1":{"1":"平台动态","2":"行业动态","3":"永康动态","4":"通知公告"},"2":{"7":"服务中心","8":"常见问题","9":"合作流程","10":"咨询与视频","11":"设备购置","12":"设备人员配置","13":"设备人员要求"},"3":{"5":"政策法规"},"4":{"6":"系统公告"}};
var _ue = UE.getEditor('editor',{readonly:true});
dataInit();
function dataInit() {
    var id =$.getUrlParam('id');
    $.ajax({
        url:'api/v1/p/mms/article/load',
        type:'post',
        data:{
            id:id
        },
        dataType:'json',
        success:function (result) {
            if(result.code=="200"){
                var data = result.data;
                categoryInit(data.category);
                typeInit(data.category,data.type+'');
                formInit(data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function formInit(data) {
    $('#cancel').removeClass('diyCancel');
    if(data.picturePath==null||data.picturePath==''){
    }else{
        $("#main_form img").attr("src",data.picturePath);
    }
    if(data.isHead==1){
        $("#is_head").attr("checked","checked");
    }else{
        $("#is_head").attr("checked",false);
    }
    $("#title").val(data.title);
    $("#url").val(data.url);
    $("#auther").val(data.auther);
    $("#summary").val(data.summary);
    $("#keywords").val(data.keywords);
    $("#stat").html("");
    if(data.stat=="1"){
        $("#stat").append("<span style='color:green'>已发布</span>")
    }else{
        $("#stat").append("<span style='color:red'>未发布</span>")
    }
    $('#releaseTime').val(data.releaseTime);
    _ue.ready(function() {
        if(data.content){
            _ue.setContent(data.content);
        }
    });
}
function categoryInit(k) {
    $("#category").html('');
    $.each(news_category,function (key,value) {
        if(key==k){
            $("#category").append("<option value='"+key+"' selected>"+value+"</option>")
        }else{
            $("#category").append("<option value='"+key+"'>"+value+"</option>")
        }
    })
}
function typeInit(parentId,k) {
    $("#type").html('');
    $.each(mapping[parentId],function (key,value) {
        if(key==k){
            $("#type").append("<option value='"+key+"' selected>"+value+"</option>")
        }else{
            $("#type").append("<option value='"+key+"'>"+value+"</option>")
        }
    })
}

